package cn.lvhang.org.service.impl;

import cn.lvhang.basic.exception.BusinessException;
import cn.lvhang.basic.service.impl.BaseServiceImpl;
import cn.lvhang.org.domain.Department;
import cn.lvhang.org.domain.Employee;
import cn.lvhang.org.domain.Shop;
import cn.lvhang.org.mapper.DepartmentMapper;
import cn.lvhang.org.mapper.EmployeeMapper;
import cn.lvhang.org.mapper.ShopMapper;
import cn.lvhang.org.service.IDepartmentService;
import cn.lvhang.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)//不支持事务
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //检验两次密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致！");
        }

        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if(employee != null){
            throw new BusinessException("账户已经存在! 请重新注册");
        }
        // 保存员工信息返回自增长id
        employeeMapper.save(admin);
        // 保存店铺信息返回自增id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        // 修改员工的所属店铺ID
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
